package cn.com.demo.i18n.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Component;

import java.util.Locale;

@Component
public class MessageSourceUtils {

    // 本地化信息，需要与session 绑定
    private Locale locale = Locale.getDefault();

    @Autowired
    private MessageSource messageSource;


    public void changeLocale(String language, String country) {
        this.locale = new Locale(language,country);
    }
    public void changeLocale(Locale locale) {
        this.locale = locale;
    }
    public String getMessage(String code) {
        return getMessage(code,null,locale);
    }
    public String getMessage(String code, Object[] args) {
        return getMessage(code,args,locale);
    }
    public String getMessage(String code, Object[] args, Locale locale) {
        return messageSource.getMessage(code,args,locale);
    }

    public String getLocale() {
        return locale.toString();
    }
}
